//
//  MTRecordVC.m
//  met
//
//  Created by 李晓俊 on 2019/5/11.
//  Copyright © 2019 李晓俊. All rights reserved.
//

#import "MTRecordVC.h"
#import "MTRecordCell.h"
@interface MTRecordVC ()
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic,strong) UITableView *tableview;

@end

@implementation MTRecordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"兑换记录";
    self.dataArr = [NSMutableArray new];
//    [self setupTableView:UITableViewStylePlain header:NO footer:YES];
    
    self.tableview = [[UITableView alloc] initWithFrame: CGRectMake(0,  NavHeight , kScreenWidth, kScreenHeight  - NavHeight -BottomHeight) style:UITableViewStyleGrouped];
    //    self.tableView = [UITableView new];
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    [self.view addSubview:self.tableview];
    
    self.tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.page++;
        [self loadData];
    }];
    self.tableview.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];
    self.tableview.backgroundColor = KBackColor;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self getInfo];

}
- (void)loadData {
//    if (self.tableView.mj_header) {
//        [self.tableView.mj_header endRefreshing];
//    }
    if (self.tableview.mj_footer) {
        [self.tableview.mj_footer endRefreshing];
    }
    [self getInfo];
}
- (void)getInfo {
    //    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    AccountService *acc = [AccountService getObject];
    acc.httpRequestResultBlock = ^(id data,int code){
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (code == 1) {
            if (self.page == 1) {
                [self.dataArr removeAllObjects];
                self.dataArr = [NSMutableArray arrayWithArray:data];
            }else {
                NSArray *info = data;
                if (info.count == 0) {
                    self.page --;
                    [self.tableview.mj_footer endRefreshingWithNoMoreData];
                }else if (info.count < 10) {
                    [self.tableview.mj_footer endRefreshingWithNoMoreData];
                }
                for (RecordModel *model in info) {
                    [self.dataArr  addObject:model];
                }
            }
           [self.tableview reloadData];
        }else {
            [self.view maketishiToast:data];
        }
    };
    [acc findExchangeRecords:[NSString stringWithFormat:@"%ld",(long)self.page]];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MTRecordCell *cell = [MTRecordCell cellWithTableView:tableView];
    cell.model = [self.dataArr objectAtIndex:indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *v = [UIView new];
    return v;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01f;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [UIView new];
    return view;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
@end
